package com.atguigu.gmall.realtime.utils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * Author: Felix
 * Date: 2022/1/23
 * Desc: 操作日期的工具类
 * SimpleDateFormat 存在线程安全问题
 * 推荐使用jdk1.8之后，推出的时间相关的类对日期进行转换
 *  DateTimeFormatter=====SimpleDateFormat
 *  LocalDateTime    =====Date
 *  Instant          =====Calendar
 */
public class DateTimeUtil {
    private static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    //将日期对象转换为字符串
    public static String toYMDHMS(Date date){
        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());

        return dtf.format(localDateTime);
    }
    //将字符串日期转换为时间毫秒数
    public static Long toTs(String dateStr){
        LocalDateTime localDateTime = LocalDateTime.parse(dateStr, dtf);
        Instant instant = localDateTime.toInstant(ZoneOffset.of("+8"));
        Long ts = instant.toEpochMilli();
        return ts;
    }

    public static void main(String[] args) {
        System.out.println(ZoneId.systemDefault());
    }
}
